package com.sdstc.modules.vo;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.ruoyi.common.enums.MechanismType;
import com.sdstc.modules.eneity.OrgEntity;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author ：wk
 * @date ：Created in 2022/10/21 14:52
 */
@Data
public class OrgDetailVo implements Serializable {

    private static final long serialVersionUID=1L;

    private String id;

    private String pid;

    private String orgName;

    // 机构级别
    private String orgLevel;

    // 机构类型
    private String orgType;

    // 实际编制总数
    private Integer realOrgan = 0;

    //高配人数
    private Integer highPersonCount = 0;

    // 实际人数
    private Integer realPerson = 0;

    /**
     * 预设编制
     */
    // 行政
    private Organization organizationAdmin = new Organization();
    // 单列
    private Organization organizationSingle = new Organization();
    // 工勤
    private Organization organizationWork = new Organization();
    // 事业
    private Organization organizationCause = new Organization();


    // 副局
    private Mechanism bureauDeputy = new Mechanism();

    // 正处级
    private Mechanism division = new Mechanism();

    // 副处级
    private Mechanism divisionDeputy = new Mechanism();

    // 正科级
    private Mechanism department = new Mechanism();

    // 副科级
    private Mechanism departmentDeputy = new Mechanism();

    // 股级
    private Mechanism group = new Mechanism();



    /**
     * 预设机构 end
     */

    /**
     * 职数及配备 start
     */
    // 部门领导正职
    private Positions departmentLead = new Positions();

    // 部门领导副职
    private Positions departmentLeadDeputy = new Positions();

    // 与部门领导同级别的其他领导
    private Positions sameDepartmentLead = new Positions();

    // 内设机构领导正职
    private Positions internalLead = new Positions();

    // 内设机构领导副职
    private Positions internalLeadDeputy = new Positions();

    // 与内设机构领导同级别的其他领导
    private Positions sameInternalLead = new Positions();




    @Data
    public static class Organization{
        private int plan = 0;

        private int existing = 0;

        private int state = 0;

        public Organization(){

        }

        public Organization(Integer plan,Integer existing){
            this.plan = plan;
            this.existing = existing;
            this.state = existing - plan;
        }

        public static Organization buildInstance(Integer plan,Integer existing){
            Organization organization = new Organization( Objects.isNull(plan) ? 0 : plan, Objects.isNull(existing) ? 0 : existing);
            return organization;
        }
    }

    @Data
    public static class Positions{
        private int plan = 0;

        private int existing = 0;

        private int state = 0;

        public Positions(){

        }

        public Positions(Integer plan,Integer existing){
            this.plan = plan;
            this.existing = existing;
            this.state = existing - plan;
        }


        public Positions(Integer existing){
            this.plan = 0;
            this.existing = existing;
            this.state = 0;
        }
    }



    @Data
    public static class Mechanism{

        // 内设
        private Integer internal = 0;

        // 派出
        private Integer dispatch = 0;

        // 下设
        private Integer under = 0;

        // 事业
        private Integer cause = 0;

        public Mechanism(){

        }

        public Mechanism(List<OrgTreeVo> list){
            Map<String, List<OrgTreeVo>> type2list =
                    list.stream().filter(o -> Objects.nonNull(o.getOrgType())).collect(Collectors.groupingBy(OrgTreeVo::getOrgType));
            this.internal = CollectionUtils.isNotEmpty(type2list.get(MechanismType.INTERNAL.getCode()))?
                    type2list.get(MechanismType.INTERNAL.getCode()).size():0;

            this.dispatch = CollectionUtils.isNotEmpty(type2list.get(MechanismType.DISPATCH.getCode()))?
                    type2list.get(MechanismType.DISPATCH.getCode()).size():0;

            this.under = CollectionUtils.isNotEmpty(type2list.get(MechanismType.UNDER.getCode()))?
                    type2list.get(MechanismType.UNDER.getCode()).size():0;

            this.cause = CollectionUtils.isNotEmpty(type2list.get(MechanismType.CAUSE.getCode()))?
                    type2list.get(MechanismType.CAUSE.getCode()).size():0;
        }

        public static Mechanism buildMechanism(List<OrgTreeVo> list){
            Mechanism mechanism = new Mechanism(list);
            return mechanism;
        }
    }




    private static OrgDetailVo getInstance(OrgEntity orgEntity){
        OrgDetailVo orgTreeVo = new OrgDetailVo();
        BeanUtils.copyProperties(orgEntity,orgTreeVo);
        return orgTreeVo;
    }
}
